/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package _bai02_phantichphankhaibao;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 * //(VM i TH {1 .. n-1}. TT j TH{1 .. n-2}. (x[i] > x[j]))
 * @author Administrator
 */
public class jClass_BieuThucLuongTu implements Interface_CacThanhPhanDacTa{
    private jClass_Bien _BienTraVe = new jClass_Bien();
    private ArrayList<jClass_PhanKhaiBaoBieuThucLuongTu> _cacPhanKhaiBao = new ArrayList<jClass_PhanKhaiBaoBieuThucLuongTu>();
    private jClass_TruongHopDacTa _CacDieuKien = new jClass_TruongHopDacTa();
    private String _BieuThucDinhDang = "\\((.+\\.)+.+\\)";

    public jClass_BieuThucLuongTu(jClass_Bien bien){
        _BienTraVe = bien;
        _BieuThucDinhDang = bien.getTen() + "\\s=\\s" + _CacDieuKien;
    }
    public void jCapNhatTuChuoi(String Str_KhaiBao) {
        //throw new UnsupportedOperationException("Not supported yet.");
        Str_KhaiBao = Str_KhaiBao.trim().substring(1, Str_KhaiBao.length() - 1);
        while(Str_KhaiBao.contains("..")){
            Str_KhaiBao.trim();
            int doDaiPhanKhaiBaoBieuThucLuongTu = Str_KhaiBao.indexOf(".", Str_KhaiBao.indexOf("..") + "..".length());
            String chuoiKhaiBaoBieuThucLuongTu = Str_KhaiBao.substring(0, doDaiPhanKhaiBaoBieuThucLuongTu);
            //JOptionPane.showMessageDialog(null, chuoiKhaiBaoBieuThucLuongTu);
            Str_KhaiBao = Str_KhaiBao.substring(doDaiPhanKhaiBaoBieuThucLuongTu + 1);
            jClass_PhanKhaiBaoBieuThucLuongTu phanKhaiBaoBieuThucLuongTu = new jClass_PhanKhaiBaoBieuThucLuongTu();
            phanKhaiBaoBieuThucLuongTu.jCapNhatTuChuoi(chuoiKhaiBaoBieuThucLuongTu);
            getCacPhanKhaiBao().add(phanKhaiBaoBieuThucLuongTu);
        }
        setCacDieuKien(new jClass_TruongHopDacTa());
        if (!Str_KhaiBao.startsWith("("))
            Str_KhaiBao = "(" + Str_KhaiBao + ")";
        getCacDieuKien().jCapNhatTuChuoi(Str_KhaiBao.trim());
    }

    public Boolean jKiemTraChuoiHopDinhDang(String str_ChuoiCanKienTra) {
        Pattern pat=Pattern.compile(getBieuThucDinhDang());
        Matcher matcher =pat.matcher(str_ChuoiCanKienTra);
        return matcher.matches();
    }

    public String jTaoBienKetQua (String tenGoc, int soTT){
        String Kq = tenGoc;
        if (soTT > 0)
            Kq += soTT;
        return Kq;
    }
    public String jToCCode(int DoLui) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "";
        jClass_PhanKhaiBaoBieuThucLuongTu phanKhaiBao = new jClass_PhanKhaiBaoBieuThucLuongTu();
        int i = 0;
        for (;i<getCacPhanKhaiBao().size(); i++){
            phanKhaiBao = getCacPhanKhaiBao().get(i);
            Kq += jClass_BoTaoCode.jTaoKhaiBaoBien(DoLui, getBienTraVe(), false, "");
            if(i != 0)
                Kq += i;
            //Kq
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    Kq += " = 0;\n";
                    break;
                }
                case VoiMoi:{
                    Kq += " = 1;\n";
                    break;
                }
            }
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui);
            Kq += phanKhaiBao.jToCCode(DoLui, jTaoBienKetQua(_BienTraVe.getTen(), i)) + "\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui++) + "{\n";
            /**
             * Kq = 0;
             * for (int i = 0; i < n - 1; i++)
             * {
             */
        }
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui);
            //Kq += "if(!" + getCacDieuKien().jToCCode(DoLui++) + ")\n";
            Kq += "if (";
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    break;
                }
                case VoiMoi:{
                    Kq += "!";
                    break;
                }
            }
            for (jClass_BieuThucLogic bieuThucLogic : _CacDieuKien.getCacDieuKien()){
                Kq+=bieuThucLogic.jToCCode(DoLui) + " && ";
            }
            Kq = Kq.substring(0, Kq.lastIndexOf(" && ")) + ")\n";
            //if (!(x[i] > x[j] && x[k] > x[l]))
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1);
            String bien = jTaoBienKetQua(_BienTraVe.getTen(), --i);
            Kq += bien;
            /*
            Kq += _BienTraVe.getTen();
            if (--i > 0)
                Kq += i;
             * */
            Kq += " = !";
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ";\n";
            //Kq2 = !Kq2;
        for(;i >0; i--){
            phanKhaiBao = getCacPhanKhaiBao().get(i);
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "if(";
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    Kq += "!";
                    break;
                }
                case VoiMoi:{
                    break;
                }
            }
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ")\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1);
            bien = jTaoBienKetQua(_BienTraVe.getTen(), --i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (--i > 0)
                Kq += i;*/
            Kq += " = !";
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ";\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
        }
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
            if (_cacPhanKhaiBao.size() % 2 == 0)
                Kq = Kq.substring(0, Kq.lastIndexOf("}")) + "\n";
        //Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "return " + _BienTraVe.getTen() + ";\n";
        return Kq;
    }

    public String jCSharpCode(int DoLui) {
       //throw new UnsupportedOperationException("Not supported yet.");
        String Kq = "";
        jClass_PhanKhaiBaoBieuThucLuongTu phanKhaiBao = new jClass_PhanKhaiBaoBieuThucLuongTu();
        int i = 0;
        for (;i<getCacPhanKhaiBao().size(); i++){
            phanKhaiBao = getCacPhanKhaiBao().get(i);
            Kq += jClass_BoTaoCode.jTaoKhaiBaoBien(DoLui, getBienTraVe(), false, "");
            if(i != 0)
                Kq += i;
            //Kq
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    Kq += " = 0;\n";
                    break;
                }
                case VoiMoi:{
                    Kq += " = 1;\n";
                    break;
                }
            }
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui);
            Kq += phanKhaiBao.jToCSharpCode(DoLui, jTaoBienKetQua(_BienTraVe.getTen(), i)) + "\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui++) + "{\n";
            /**
             * Kq = 0;
             * for (int i = 0; i < n - 1; i++)
             * {
             */
        }
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui);
            //Kq += "if(!" + getCacDieuKien().jToCCode(DoLui++) + ")\n";
            Kq += "if (";
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    break;
                }
                case VoiMoi:{
                    Kq += "!";
                    break;
                }
            }
            for (jClass_BieuThucLogic bieuThucLogic : _CacDieuKien.getCacDieuKien()){
                Kq+=bieuThucLogic.jToCCode(DoLui) + " && ";
            }
            Kq = Kq.substring(0, Kq.lastIndexOf(" && ")) + ")\n";
            //if (!(x[i] > x[j] && x[k] > x[l]))
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1);
            String bien = jTaoBienKetQua(_BienTraVe.getTen(), --i);
            Kq += bien;
            /*
            Kq += _BienTraVe.getTen();
            if (--i > 0)
                Kq += i;
             * */
            Kq += " = !";
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ";\n";
            //Kq2 = !Kq2;
        for(;i >0; i--){
            phanKhaiBao = getCacPhanKhaiBao().get(i);
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "if(";
            switch (phanKhaiBao.getLoaiBieuThuc()){
                case TonTai:{
                    Kq += "!";
                    break;
                }
                case VoiMoi:{
                    break;
                }
            }
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ")\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(DoLui + 1);
            bien = jTaoBienKetQua(_BienTraVe.getTen(), --i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (--i > 0)
                Kq += i;*/
            Kq += " = !";
            bien = jTaoBienKetQua(_BienTraVe.getTen(), i);
            Kq += bien;/*
            Kq += _BienTraVe.getTen();
            if (i > 0)
                Kq += i;*/
            Kq += ";\n";
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
        }
            Kq += jClass_BoTaoCode.jTaoCapDo(--DoLui) + "}\n";
            if (_cacPhanKhaiBao.size() % 2 == 0)
                Kq = Kq.substring(0, Kq.lastIndexOf("}")) + "\n";
        //Kq += jClass_BoTaoCode.jTaoCapDo(DoLui) + "return " + _BienTraVe.getTen() + ";\n";
        return Kq;
    }

    public String jVBNetCode(int DoLui) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the _BienTraVe
     */
    public jClass_Bien getBienTraVe() {
        return _BienTraVe;
    }

    /**
     * @param BienTraVe the _BienTraVe to set
     */
    public void setBienTraVe(jClass_Bien BienTraVe) {
        this._BienTraVe = BienTraVe;
    }

    /**
     * @return the _cacPhanKhaiBao
     */
    public ArrayList<jClass_PhanKhaiBaoBieuThucLuongTu> getCacPhanKhaiBao() {
        return _cacPhanKhaiBao;
    }

    /**
     * @param cacPhanKhaiBao the _cacPhanKhaiBao to set
     */
    public void setCacPhanKhaiBao(ArrayList<jClass_PhanKhaiBaoBieuThucLuongTu> cacPhanKhaiBao) {
        this._cacPhanKhaiBao = cacPhanKhaiBao;
    }

    /**
     * @return the _CacDieuKien
     */
    public jClass_TruongHopDacTa getCacDieuKien() {
        return _CacDieuKien;
    }

    /**
     * @param CacDieuKien the _CacDieuKien to set
     */
    public void setCacDieuKien(jClass_TruongHopDacTa CacDieuKien) {
        this._CacDieuKien = CacDieuKien;
    }

    /**
     * @return the _BieuThucDinhDang
     */
    public String getBieuThucDinhDang() {
        return _BieuThucDinhDang;
    }

    /**
     * @param BieuThucDinhDang the _BieuThucDinhDang to set
     */
    public void setBieuThucDinhDang(String BieuThucDinhDang) {
        this._BieuThucDinhDang = BieuThucDinhDang;
    }
}
